package Day7;

import org.junit.Test;

import java.util.Arrays;

public class test03{  // 添加public class包装

    @Test
    public void test02() {
        // 实例化学生数组
        Student[] array = {
                new Student(1003, "Tom", "Computer Science"),
                new Student(1001, "Alice", "Mathematics"),
                new Student(1005, "Bob", "Physics"),
                new Student(1002, "Charlie", "Chemistry"),
                new Student(1004, "David", "Biology")
        };

        // 使用默认排序规则（按学号升序）
        Arrays.sort(array);
        System.out.println("按学号升序排列：");
        System.out.println(Arrays.toString(array));

        // 使用Lambda表达式按姓名长度排序
        Arrays.sort(array, (s1, s2) -> s1.getName().length() - s2.getName().length());
        System.out.println("按姓名长度排序：");
        System.out.println(Arrays.toString(array));
    }

    private static class Student implements Comparable<Student> {  // 添加static修饰符
        private int id;
        private String name;
        private String major;

        public Student(int id, String name, String major) {
            this.id = id;
            this.name = name;
            this.major = major;
        }

        public String getName() {
            return name;
        }

        @Override
        public int compareTo(Student other) {
            return Integer.compare(this.id, other.id);
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", major='" + major + '\'' +
                    '}';
        }
    }
}
